// 云函数入口文件
const cloud = require("wx-server-sdk");

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});

const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  const { OPENID } = cloud.getWXContext();

  try {
    // 根据商家openid查询商家信息
    const result = await db
      .collection("merchants")
      .where({
        _openid: OPENID,
      })
      .get();

    if (result.data.length > 0) {
      return {
        code: 0,
        msg: "获取成功",
        data: result.data[0],
      };
    } else {
      return {
        code: -1,
        msg: "未找到商家信息",
      };
    }
  } catch (err) {
    console.error("获取商家信息失败：", err);
    return {
      code: -1,
      msg: "获取失败",
      error: err,
    };
  }
};
